import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { SEntityInfo } from "../../../Message/BroadCast/SEntityInfo";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
import { GameProxy } from "../mvc/GameProxy";
import { SPetArenaAccumulateRewardInfo } from "../../../Message/Game/SPetArenaAccumulateRewardInfo";
import { MsgTipInfo } from "../model/MsgTipInfo";
import { PetCpnGetRewardTipPanel } from "../view/pet/petCompetition/PetCpnGetRewardTipPanel";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { IView } from "../../mvc/interfaces/IView";
import { PetCompetitionModule } from "../../../modules/PetCompetitionModule";
import { PetCpnBattleWindow } from "../view/pet/petCompetition/PetCpnBattleWindow";
import { PetCpnIconView } from "../view/pet/petCompetition/PetCpnIconView";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { SPetArenaInfo } from "../../../Message/Game/SPetArenaInfo";
import { SPetUpdate } from "../../../Message/Game/SPetUpdate";
import { EPlayerItemPosType } from "../../../Message/Public/EPlayerItemPosType";
import { SPlayerPetArena } from "../../../Message/Game/SPlayerPetArena";
import { SPlayerPetArenaEx } from "../../../Message/Game/SPlayerPetArenaEx";
import { SPetArenaInfos } from "../../../Message/Game/SPetArenaInfos";
import { EPetArenaSearch } from "../../../Message/Game/EPetArenaSearch";
import { EPetArenaChallenge } from "../../../Message/Game/EPetArenaChallenge";
import { SPetArenaResult } from "../../../Message/Game/SPetArenaResult";
import { SPetInfo } from "../../../Message/Game/SPetInfo";
import { SMyPetChallengeLog } from "../../../Message/Game/SMyPetChallengeLog";
import { SPetArenaChallengeEnd } from "../../../Message/Game/SPetArenaChallengeEnd";
import { IPetCompetitionModule } from "../../../modules/interfaces/IPetCompetitionModule";
type int = number;
//class PetCompetitionController
    
    export  class PetCompetitionController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.PetCpnOpenWindow, this.onPetCpnOpenWindowHandler);
            Dispatcher.addEventListener(EventName.PetCpnSearch, this.onPetCpnSearchHandler);
            Dispatcher.addEventListener(EventName.PetCpnJoin, this.onPetCpnJoinHandler);
            Dispatcher.addEventListener(EventName.PetCpnGetMy, this.onPetCpnGetMyHandler);
            Dispatcher.addEventListener(EventName.PetCpnLock, this.onPetCpnLockHandler);
            Dispatcher.addEventListener(EventName.PetCpnGetLog, this.onPetCpnGetLogHandler);
            Dispatcher.addEventListener(EventName.PetCpnView, this.onPetCpnViewHandler);
            Dispatcher.addEventListener(EventName.PetCpnAttack, this.onPetCpnAttackHandler);
            Dispatcher.addEventListener(EventName.PetCpnAttackResponse, this.onPetCpnAttackResponseHandler);
            Dispatcher.addEventListener(EventName.PetCpnObserve, this.onPetCpnObserveHandler);
            Dispatcher.addEventListener(EventName.PetCpnGetReward, this.onPetCpnGetRewardHandler);
            Dispatcher.addEventListener(EventName.PetCpnViewBattle, this.onPetCpnViewBattleHandler);
            Dispatcher.addEventListener(EventName.PetCpnShowIcon, this.onPetCpnShowIconHandler);
            Dispatcher.addEventListener(EventName.PetCpnBuyTime, this.onPetCpnBuyTimeHandler);
            Dispatcher.addEventListener(EventName.PetCpnInsure, this.onPetCpnInsureHandler);
            Dispatcher.addEventListener(EventName.PetCpnEntityUpdate, this.onPetCpnEntityUpdateHandler);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            flash.utils.setTimeout(this.addCanGetCheck, 10000);
            return;
        }

        /* internal  */onPetCpnEntityUpdateHandler(arg1: DataEvent): void
        {
            if (this._cpnBattleWindow && !this._cpnBattleWindow.isHide) 
            {
                this._cpnBattleWindow.petCpnEntityUpdate(arg1.data as SEntityInfo);
            }
            return;
        }

        /* internal  */addCanGetCheck(): void
        {
            if (cache.role.entityInfo.level >= 65 && !cache.petCompetition.isInOver) 
            {
                if (!this._checkTimer) 
                {
                    this._checkTimer = new SecTimer(60);
                    this._checkTimer.addListener(TimerType.ENTERFRAME, this.onTimerHandler);
                }
                this._checkTimer.start();
            }
            return;
        }

        /* internal  */onTimerHandler(arg1: SecTimer): void
        {
            if (cache.role.entityInfo.level >= 65 && !cache.petCompetition.isInOver) 
            {
                GameProxy.petCpn.getAccumulateRewardInfo();
            }
            return;
        }

        /* internal  */onPetCpnCanGetRewardCommandHandler(arg1: SPetArenaAccumulateRewardInfo): void
        {
            var loc1=null;
            if (arg1 && arg1.canGet) 
            {
                loc1 = new MsgTipInfo();
                if (this._getRewardTipPanel == null) 
                {
                    this._getRewardTipPanel = new PetCpnGetRewardTipPanel();
                }
                this._getRewardTipPanel.update(arg1);
                loc1.displayObject = this._getRewardTipPanel;
                loc1.type = 1;
                Dispatcher.dispatchEvent(new DataEvent(EventName.MsgTipDateChange, loc1));
            }
            return;
        }

        /* internal  */onPetCpnOpenWindowHandler(arg1: DataEvent): void
        {
            if (cache.petCompetition.isInOver) 
            {
                if (cache.petCompetition.isOverDate) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(54730));
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(54729));
                }
                return;
            }
            if (cache.role.entityInfo.level < 65) 
            {
                return;
            }
            if (!cache.petCompetition.isInBattle && !cache.petCompetition.battleIsShow) 
            {
                view.show();
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetCpnViewBattle));
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.PetCpnPlayerCommand, this.onPetCpnPlayerCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnPlayerExCommand, this.onPetCpnPlayerExCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnSearchCommand, this.onPetCpnSearchCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnJoinResultCommand, this.onPetCpnJoinResultCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnChallengeResultCommand, this.onPetCpnChallengeResultCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnDetailInfoCommand, this.onPetCpnDetailInfoCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnDetailInfoResultCommand, this.onPetCpnDetailInfoResultCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnLogCommand, this.onPetCpnLogCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnChallengeEndCommand, this.onPetCpnChallengeEndCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnPetCoinCommand, this.onPetCpnPetCoinCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnCountRewardStartCommand, this.onPetCpnCountRewardStartCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnCountRewardEndCommand, this.onPetCpnCountRewardEndCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetCpnCanGetRewardCommand, this.onPetCpnCanGetRewardCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.ActiveArenaPetDouble, this.onActiveArenaPetDoubleHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._cpnModule) 
            {
                this._cpnModule = new PetCompetitionModule();
            }
            return this._cpnModule;
        }

        public get cpnBattleWindow(): PetCpnBattleWindow
        {
            if (!this._cpnBattleWindow) 
            {
                this._cpnBattleWindow = new PetCpnBattleWindow();
            }
            return this._cpnBattleWindow;
        }

        public get iconView(): PetCpnIconView
        {
            if (!this._iconView) 
            {
                this._iconView = new PetCpnIconView();
            }
            return this._iconView;
        }

        /* internal  */onActiveArenaPetDoubleHandler(arg1: SActiveToClient): void
        {
            if (this._iconView && !this._iconView.isHide) 
            {
                this._iconView.updateActiveToClientInfo(arg1);
            }
            return;
        }

        /* internal  */onPetCpnSearchHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["searchType"] as int;
            var loc2=arg1.data["perPage"] as int;
            var loc3=arg1.data["targetPage"] as int;
            var loc4=arg1.data["isCostMoney"] as Boolean;
            GameProxy.petCpn.search(loc1, loc2, loc3, loc4);
            return;
        }

        /* internal  */onPetCpnJoinHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.petCpn.join(loc1);
            return;
        }

        /* internal  */onPetCpnGetMyHandler(arg1: DataEvent): void
        {
            GameProxy.petCpn.getMyPetArena();
            return;
        }

        /* internal  */onPetCpnLockHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["isLock"] as Boolean;
            var loc2=arg1.data["uid"] as string;
            GameProxy.petCpn.lockAttribute(loc1, loc2);
            return;
        }

        /* internal  */onPetCpnGetLogHandler(arg1: DataEvent): void
        {
            GameProxy.petCpn.getMyPetArenaLog();
            return;
        }

        /* internal  */onPetCpnViewHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPetArenaInfo;
            GameProxy.petCpn.petArenaDetailInfo(loc1);
            return;
        }

        /* internal  */onPetCpnAttackHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["petArenaInfo"] as SPetArenaInfo;
            var loc2=arg1.data["uid"] as string;
            var loc3=arg1.data["isChangePet"] as Boolean;
            var loc4=arg1.data["challengeType"] as int;
            var loc5;
            if (!(loc5 = cache.pet.getPetInfoByUid(loc2))) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54695));
                return;
            }
            if (!cache.petCompetition.isHasAttackCount) 
            {
                this._afterBuyAttackObj = arg1.data;
                this._cpnModule.showAddTimeAlert(this.afterBuyAttack);
                MsgManager.showRollTipsMsg(Language.getString(54697));
                return;
            }
            GameProxy.petCpn.challenge(loc1, loc2, loc3, loc4);
            return;
        }

        /* internal  */onPetCpnAttackResponseHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */afterBuyAttack(): void
        {
            var loc1=this._afterBuyAttackObj["petArenaInfo"] as SPetArenaInfo;
            var loc2=this._afterBuyAttackObj["uid"] as string;
            var loc3=this._afterBuyAttackObj["isChangePet"] as Boolean;
            var loc4=this._afterBuyAttackObj["challengeType"] as int;
            GameProxy.petCpn.challenge(loc1, loc2, loc3, loc4);
            return;
        }

        /* internal  */onPetCpnObserveHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            GameProxy.petCpn.observe(loc1);
            return;
        }

        /* internal  */onPetCpnGetRewardHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["type"] as int;
            var loc2=arg1.data["callBackFun"] as Function;
            GameProxy.petCpn.getMyReward(loc1, loc2);
            return;
        }

        /* internal  */onPetCpnViewBattleHandler(arg1: DataEvent): void
        {
            GameProxy.petCpn.observe(true);
            this.cpnBattleWindow.show();
            return;
        }

        /* internal  */onPetCpnShowIconHandler(arg1: DataEvent): void
        {
            this.iconView.show();
            return;
        }

        /* internal  */onPetBagUpdateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPetUpdate;
            if (loc1 && !(loc1.updateType == 4) && (loc1.bagType == EPlayerItemPosType._EPlayerItemPosTypePet || loc1.bagType == EPlayerItemPosType._EPlayerItemPosTypePetWarehouse)) 
            {
                if (this._cpnModule && !this._cpnModule.isHide) 
                {
                    this._cpnModule.resetComboBoxData();
                }
            }
            return;
        }

        /* internal  */onPetCpnPlayerCommandHandler(arg1: SPlayerPetArena): void
        {
            if (this._cpnModule) 
            {
                this._cpnModule.updatePetCpnInfo(arg1);
            }
            return;
        }

        /* internal  */onPetCpnPlayerExCommandHandler(arg1: SPlayerPetArenaEx): void
        {
            if (this._cpnModule) 
            {
                this._cpnModule.updatePetCpnInfoEx(arg1);
            }
            return;
        }

        /* internal  */onPetCpnSearchCommandHandler(arg1: SPetArenaInfos): void
        {
            if (arg1) 
            {
                if (arg1.searchType != EPetArenaSearch._EPetArenaSearchChallengeRank) 
                {
                    if (arg1.searchType == EPetArenaSearch._EPetArenaSearchChallengeRandom || arg1.searchType == EPetArenaSearch._EPetArenaSearchFunctionalRandom) 
                    {
                        this._cpnModule.updateArrangeList(arg1, EPetArenaChallenge._EPetArenaChallengeRandom);
                    }
                    else if (arg1.searchType == EPetArenaSearch._EPetArenaSearchHero || arg1.searchType == EPetArenaSearch._EPetArenaSearchLocalHero) 
                    {
                        this._cpnModule.updateTopList(arg1);
                    }
                    else if (arg1.searchType == EPetArenaSearch._EPetArenaSearchRward) 
                    {
                        this._cpnModule.updateDayRewardWindow(arg1);
                    }
                }
                else 
                {
                    this._cpnModule.updateArrangeList(arg1, EPetArenaChallenge._EPetArenaChallengeRank);
                }
            }
            return;
        }

        /* internal  */onPetCpnJoinResultCommandHandler(arg1: SPetArenaResult): void
        {
            if (arg1) 
            {
                if (arg1.result) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetCpnGetMy));
                }
            }
            return;
        }

        /* internal  */onPetCpnChallengeResultCommandHandler(arg1: SPetArenaResult): void
        {
            var loc1=null;
            var loc2=null;
            if (arg1.result) 
            {
                cache.petCompetition.isInBattle = true;
                this.cpnBattleWindow.show();
                if (this._cpnModule) 
                {
                    this._cpnModule.hide();
                }
                var loc3=0;
                var loc4=cache.petCompetition.petArenaExReward;
                for(loc2 of loc4) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BackPackAddTaskEntity, loc2));
                }
            }
            else 
            {
                if (this._cpnModule) 
                {
                    this._cpnModule.refreshList(1);
                }
                MsgManager.showRollTipsMsg(Language.getString(54703));
            }
            return;
        }

        /* internal  */onPetCpnDetailInfoCommandHandler(arg1: SPetInfo): void
        {
            if (arg1) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetShowInfo, arg1));
            }
            return;
        }

        /* internal  */onPetCpnDetailInfoResultCommandHandler(arg1: SPetArenaResult): void
        {
            if (arg1 && !arg1.result) 
            {
                if (this._cpnModule) 
                {
                    this._cpnModule.refreshList(1);
                    this._cpnModule.refreshList(2);
                }
            }
            return;
        }

        /* internal  */onPetCpnLogCommandHandler(arg1: SMyPetChallengeLog): void
        {
            if (this._cpnModule) 
            {
                this._cpnModule.updateLog(arg1);
            }
            return;
        }

        /* internal  */onPetCpnChallengeEndCommandHandler(arg1: SPetArenaChallengeEnd): void
        {
            cache.petCompetition.isInBattle = false;
            var loc1=cache.petCompetition.mapId;
            if (this._cpnBattleWindow) 
            {
                if (!this._cpnBattleWindow.isHide) 
                {
                    this._cpnBattleWindow.updateBattleResult(arg1);
                }
            }
            return;
        }

        /* internal  */onPetCpnPetCoinCommandHandler(arg1: int): void
        {
            if (this._cpnModule && !this._cpnModule.isHide) 
            {
                this._cpnModule.updatePetCoin(arg1);
            }
            return;
        }

        /* internal  */onPetCpnCountRewardStartCommandHandler(arg1: Boolean): void
        {
            if (this._cpnModule && !this._cpnModule.isHide) 
            {
                this._cpnModule.hide();
            }
            return;
        }

        /* internal  */onPetCpnCountRewardEndCommandHandler(arg1: Boolean): void
        {
            return;
        }

        /* internal  */onPetCpnBuyTimeHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Function;
            GameProxy.petCpn.buyExtraNum(loc1);
            return;
        }

        /* internal  */onPetCpnInsureHandler(arg1: DataEvent): void
        {
            GameProxy.petCpn.buyInsurance();
            return;
        }

        private /* var */_cpnModule: IPetCompetitionModule;

        private /* var */_cpnBattleWindow: PetCpnBattleWindow;

        private /* var */_iconView: PetCpnIconView;

        private /* var */_getRewardTipPanel: PetCpnGetRewardTipPanel;

        private /* var */_afterBuyAttackObj: Object;

        private /* var */_checkTimer: SecTimer;
    }
